package com.sun.sort.hill;


import java.util.Arrays;

//希尔排序
public class hill1 {
    public static void main(String[] args) {
        int[] arr = {8,9,1,7,2,3,5,4,6,0};
//        exchange(arr);
        shift(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void exchange(int[] arr){
        int temp = 0;
        for (int gap = arr.length / 2;gap > 0;gap /= 2 ){
            for (int i = gap; i < arr.length; i++) {
                for (int j = i - gap; j >= 0; j -= gap) {
                    if (arr[j] > arr[j + gap]){
                        temp = arr[j];
                        arr[j] = arr[j + gap];
                        arr[j + gap] = temp;
                    }
                }
            }
        }

/*        //第一轮排序
        //因为第一轮排序，是将10个数据分成5组
        for (int i = 5; i < arr.length; i++) {
            //遍历各组中所有的元素(一共五组，每组两个元素),步长5
            for (int j = i - 5; j >= 0; j -= 5) {
                //如果当前元素大于加上步长后的那个元素，说明该交换
                if (arr[j] > arr[j + 5]){
                    temp = arr[j];
                    arr[j] = arr[j + 5];
                    arr[j + 5] = temp;
                }
            }
        }
        System.out.println("shell排序第1轮，排序结果" + Arrays.toString(arr));
        for (int i = 2; i < arr.length; i++) {
            for (int j = i - 2; j >= 0; j -= 2) {
                if (arr[j] > arr[j + 2]){
                    temp = arr[j];
                    arr[j] = arr[j + 2];
                    arr[j + 2] = temp;
                }
            }
        }
        System.out.println("shell排序第2轮，排序结果" + Arrays.toString(arr));
        for (int i = 1; i < arr.length; i++) {
            for (int j = i - 1; j >= 0; j -= 1) {
                if (arr[j] > arr[j + 1]){
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println("shell排序第3轮，排序结果" + Arrays.toString(arr));*/
    }
    public static void shift(int[] arr){
        for (int gap = arr.length / 2;gap > 0;gap /= 2 ){

            for (int i = gap;i < arr.length;i++){
                int index = i;
                int temp = arr[index];
                if (temp< arr[index - gap]){
                    while ((index -gap) >= 0 && temp < arr[index - gap]){
                        arr[index] = arr[index - gap];
                        index -= gap;
                    }
                    arr[index] = temp;
                }
            }

        }
    }
}
